package com.sx.wechat.security;

import org.apache.oltu.oauth2.as.request.OAuthAuthzRequest;
import org.apache.oltu.oauth2.as.validator.CodeValidator;
import org.apache.oltu.oauth2.common.exception.OAuthProblemException;
import org.apache.oltu.oauth2.common.exception.OAuthSystemException;
import org.apache.oltu.oauth2.common.message.types.ResponseType;
import org.apache.oltu.oauth2.common.utils.OAuthUtils;
import org.apache.oltu.oauth2.common.validators.OAuthValidator;

import javax.servlet.http.HttpServletRequest;

/**
 * Created by lihao on 2018/1/16.
 */
public class SimpleOAuthAuthzRequest extends OAuthAuthzRequest {
    public SimpleOAuthAuthzRequest(HttpServletRequest request) throws OAuthSystemException, OAuthProblemException {
        super(request);
    }
    protected OAuthValidator<HttpServletRequest> initValidator() throws OAuthProblemException, OAuthSystemException {
        this.validators.put(ResponseType.CODE.toString(), CodeValidator.class);
        this.validators.put(ResponseType.TOKEN.toString(), SimpleTokenValidator.class);
        String requestTypeValue = this.getParam("response_type");
        if(OAuthUtils.isEmpty(requestTypeValue)) {
            throw OAuthUtils.handleOAuthProblemException("Missing response_type parameter value");
        } else {
            Class clazz = (Class)this.validators.get(requestTypeValue);
            if(clazz == null) {
                throw OAuthUtils.handleOAuthProblemException("Invalid response_type parameter value");
            } else {
                return (OAuthValidator)OAuthUtils.instantiateClass(clazz);
            }
        }
    }
}
